package com.czmh.etc.onlinepubsys.controller;

import com.czmh.etc.common.core.annotation.ApiVersion;
import com.czmh.etc.onlinepubsys.json.DetailJson;
import com.czmh.etc.onlinepubsys.json.JsonResult;
import com.czmh.etc.onlinepubsys.request.UploadImageRequest;
import com.czmh.etc.onlinepubsys.services.SysFileService;
import com.czmh.etc.onlinepubsys.utils.JsonResultUtil;
import com.czmh.etc.onlinepubsys.utils.ResponseJsonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.util.ClassUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @描述：SysFileController
 * @author： Seven.
 * @创建日期：Jun 11, 2019 11:16:23 PM.
 * @版本：1.0.0
 */
@RestController
@RequestMapping("/{version}/api/upload")
@Api(tags = {"文件上传管理"})
public class SysFileController {
    public Logger logger = LoggerFactory.getLogger(SysFileController.class);

    @Value("${sys.upload.imagepath}")
    private String upload_image_url;
    @Autowired
    private SysFileService sysFileService;

    private static String res_path = "res/images";

    @ApiVersion(1)
    @PostMapping(value = "/image")
    @ApiOperation(value = "上传管理/图片文件上传", notes = "上传管理/图片文件上传")
    public DetailJson<String> uploadImage(HttpServletRequest request, @RequestBody UploadImageRequest req)
            throws Exception {
        // 上传图片
        List<String> imagebase64List = new ArrayList<String>();
        imagebase64List.add(req.getImage());
        List<String> urlpaths = this.dealUpload(imagebase64List, req.getUser_id());
        String path = urlpaths.get(0);
        return ResponseJsonUtil.getDetailJson(path);
    }

    /**
     * 上传图片
     *
     * @param imagebase64List 图片base64字符串
     * @param user_id         用户id
     * @return
     */
    private List<String> dealUpload(List<String> imagebase64List, String user_id) {
        List<String> imgFileUrls = new ArrayList<String>();
        OcrController.dealBase64(imagebase64List, user_id, imgFileUrls, upload_image_url, res_path, sysFileService);
        return imgFileUrls;
    }

    @ApiVersion(1)
    @PostMapping(value = "/oss")
    @ApiOperation(value = "上传管理/上传阿里OSS", notes = "上传管理/上传阿里OSS")
    public JsonResult upload(@ApiParam(value = "用户id", required = true) String userId,
                             @ApiParam(value = "文件", required = true) @RequestPart(value = "file") MultipartFile file) throws IOException {
        return sysFileService.saveFileToOss(userId, file);
    }

    @ApiVersion(1)
    @PostMapping(value = "/appOssUpload")
    @ApiOperation(value = "小程序上传管理/小程序上传阿里OSS", notes = "小程序上传管理/小程序上传阿里OSS")
    public JsonResult appOssUpload(HttpServletRequest request, @RequestBody UploadImageRequest req)
            throws Exception {
        // 上传图片
        try {
            List<String> imagebase64List = new ArrayList<String>();
            imagebase64List.add(req.getImage());
            List<String> list = this.dealUpload(imagebase64List, req.getUser_id());
            if (CollectionUtils.isEmpty(list)) {
                return JsonResultUtil.toErrorResult("上传文件失败");
            } else {
                String path = list.get(0);
                File file = new File(System.getProperty("user.dir") + File.separator + "static" + File.separator + path);
                FileInputStream inputStream = new FileInputStream(file);
                MultipartFile multipartFile = new MockMultipartFile("file", file.getName(), "text/plain", IOUtils.toByteArray(inputStream));
                return sysFileService.saveFileToOss(req.getUser_id(), multipartFile);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JsonResultUtil.toErrorResult("上传文件失败");
    }


}